import router from "./router";
import nprogress from "nprogress";
import userStores from "@/store/modules/user";
import userStore from "@/store"
import { GET_TOKEN } from "@/utils/token";
router.beforeEach((to, from, next) => {
    nprogress.start();
    const token = GET_TOKEN();
    const userName = userStores.state.userInfo.userName;
    if (token) {
      if (to.path == "/login") {
        next({ path: "/" });
      } else {
        if (userName) {
          next();
        } else {
          try {
            userStore.dispatch('userInfo')
            next();
          } catch (error) {
            next({ path: "/login" });
          }
        }
        userStore.dispatch('userInfo')
        next();
      }
    } else {
      if (to.path == "/login") {
        next();
      } else {
        next({ path: "/login" });
      }
    }
});
